import 'package:baselib/utils/sp_util.dart';
import 'package:framelib/utils/constant_util.dart';
import 'package:get/get.dart';
import 'package:module_business/module_index/model/index_room_item_entity.dart';

import 'user_entity.dart';
import 'package:nim_core/nim_core.dart';

/**
 * 用户信息全局基础管理类
 */
class UserParamsModel extends GetxController {
  static final UserParamsModel _singleton = UserParamsModel._internal();

  static UserParamsModel get instance =>
      Get.put<UserParamsModel>(UserParamsModel());

  factory UserParamsModel() {
    return _singleton;
  }

  UserParamsModel._internal() {
    //做一些操作
  }

  //设置全局用户信息下的操作信息
  String _account;
  String _password;
  String _token;
  String _userId;
  String _imToken;

  String _yxAccid;

  String _favorite;

  UserEntity _userEntity;
  NIMUser _selfNIMUser;
  IndexRoomItemEntity _myRoom;

  //黑名单列表
  List<String> _myBlackIds;

  //我关注的人员列表
  List<String> _myFollowIds;

  // //清空
  clear() {
    setAccount('');
    setPassword('');
    setToken('');
    setUserId('');
    setImToken('');
    setYxAccid('');
    setFavorite('');
    setUserEntity(null);
    setMyRoom(null);
    setSelfNIMUser();
    setMyBlackIds([]);
    setMyFollowIds([]);
  }

  //
  String get account => (_account ?? SpUtil.getString(ConstantUtil.account));

  String get password => (_password ?? SpUtil.getString(ConstantUtil.password));

  String get token => (_token ?? SpUtil.getString(ConstantUtil.token));

  String get userId => (_userId ?? SpUtil.getString(ConstantUtil.userId));

  String get imToken => (_imToken ?? SpUtil.getString(ConstantUtil.imToken));

  String get yxAccid => (_yxAccid ?? SpUtil.getString(ConstantUtil.yxAccid));

  String get favorite => (_favorite ?? SpUtil.getString(ConstantUtil.favorite));

  UserEntity get userEntity => (_userEntity ??
      UserEntity.fromJson(SpUtil.getObject(ConstantUtil.userEntity) ?? {}));

  NIMUser get selfNIMUser => (_selfNIMUser ??
      NIMUser.fromMap(SpUtil.getObject(ConstantUtil.selfNIMUser) ?? {}));

  IndexRoomItemEntity get myRoom => (_myRoom ??
      IndexRoomItemEntity.fromJson(SpUtil.getObject(ConstantUtil.myRoom) ?? {}));

  List<String> get myBlackIds => (_myBlackIds ??SpUtil.getStringList(ConstantUtil.myBlackIds) ?? []);
  List<String> get myFollowIds => (_myFollowIds ??SpUtil.getStringList(ConstantUtil.myBlackIds) ?? []);

  UserParamsModel setUserId(String value) {
    _userId = value;
    SpUtil.putString(ConstantUtil.userId, value);
    update();
    return this;
  }

  UserParamsModel setAccount(String value) {
    _account = value;
    SpUtil.putString(ConstantUtil.account, value);
    update();
    return this;
  }

  UserParamsModel setPassword(String value) {
    _password = value;
    SpUtil.putString(ConstantUtil.password, value);
    update();
    return this;
  }

  UserParamsModel setToken(String value) {
    _token = value;
    SpUtil.putString(ConstantUtil.token, value);
    update();
    return this;
  }

  UserParamsModel setImToken(String value) {
    _imToken = value;
    SpUtil.putString(ConstantUtil.imToken, value);
    update();
    return this;
  }

  UserParamsModel setYxAccid(String value) {
    _yxAccid = value;
    SpUtil.putString(ConstantUtil.yxAccid, value);
    update();
    return this;
  }

  UserParamsModel setFavorite(String value) {
    _favorite = value;
    SpUtil.putString(ConstantUtil.favorite, value);
    update();
    return this;
  }

  UserParamsModel setSelfNIMUser(
      {userIdTemp, nickTemp, avatarTemp, signTemp, int genderTemp,
        emailTemp, birthTemp, mobileTemp, extTemp}) {
    var nimUser = selfNIMUser;
    if(nimUser==null)nimUser = NIMUser();
    _selfNIMUser = nimUser;
    if((userIdTemp??'').isNotEmpty)_selfNIMUser.userId = userIdTemp;
    if((nickTemp??'').isNotEmpty)_selfNIMUser.nick = nickTemp;
    if((avatarTemp??'').isNotEmpty)_selfNIMUser.avatar = avatarTemp;
    if((signTemp??'').isNotEmpty)_selfNIMUser.sign = signTemp;
    if(genderTemp!= null)_selfNIMUser.gender = (genderTemp==0?NIMUserGenderEnum.male:NIMUserGenderEnum.female);
    if((emailTemp??'').isNotEmpty)_selfNIMUser.email = emailTemp;
    if((birthTemp??'').isNotEmpty)_selfNIMUser.birth = birthTemp;
    if((mobileTemp??'').isNotEmpty)_selfNIMUser.mobile = mobileTemp;
    if((extTemp??'').isNotEmpty)_selfNIMUser.ext = extTemp;

    SpUtil.putObject(ConstantUtil.selfNIMUser, _selfNIMUser?.toMap() ?? {});
    update();
    return this;
  }
  UserParamsModel setSelfNIMUserNull() {
    _selfNIMUser = null;
    SpUtil.putObject(ConstantUtil.selfNIMUser, _selfNIMUser?.toMap() ?? {});
    update();
    return this;
  }

  UserParamsModel setUserEntity(UserEntity value) {
    _userEntity = value;
    SpUtil.putObject(ConstantUtil.userEntity, value?.toJson() ?? {});
    update();
    return this;
  }

  UserParamsModel setMyRoom(IndexRoomItemEntity value) {
    _myRoom = value;
    SpUtil.putObject(ConstantUtil.myRoom, value?.toJson() ?? {});
    update();
    return this;
  }
  UserParamsModel setMyBlackIds(List<String> value) {
    _myBlackIds = value;
    SpUtil.putStringList(ConstantUtil.myBlackIds, value??[]);
    update();
    return this;
  }
  UserParamsModel setMyFollowIds(List<String> value) {
    _myFollowIds = value;
    SpUtil.putStringList(ConstantUtil.myFollowIds, value??[]);
    update();
    return this;
  }
}
